/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDlg.java
 *
 * Created on May 5, 2010, 7:49:18 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ManagementBiz;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class LoginDlg extends javax.swing.JDialog {

    public static CampaignMainFrm main;
    ManagementBiz manaBuz = new ManagementBiz();

    public LoginDlg(CampaignMainFrm main, boolean modal) {
        super(main, modal);
        initComponents();
        LoginDlg.main = main;
        setLocationRelativeTo(main);
        //this.setVisible(true);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lblLogin = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        pwfPassword = new javax.swing.JPasswordField();
        txfUserName = new javax.swing.JTextField();
        btnLogin = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Login");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        lblLogin.setFont(new java.awt.Font("Tahoma", 1, 20));
        lblLogin.setForeground(new java.awt.Color(51, 102, 255));
        lblLogin.setText("Campaign Information");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 28, 0);
        getContentPane().add(lblLogin, gridBagConstraints);

        lblUserName.setText("UserName : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 23;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 10, 0);
        getContentPane().add(lblUserName, gridBagConstraints);

        lblPassword.setText("Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 26;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 10, 0);
        getContentPane().add(lblPassword, gridBagConstraints);

        pwfPassword.setColumns(10);
        pwfPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwfPasswordKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 88;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 20);
        getContentPane().add(pwfPassword, gridBagConstraints);

        txfUserName.setColumns(10);
        txfUserName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txfUserNameKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 88;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 20);
        getContentPane().add(txfUserName, gridBagConstraints);

        btnLogin.setBackground(new java.awt.Color(51, 102, 255));
        btnLogin.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnLogin.setForeground(new java.awt.Color(255, 255, 255));
        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
        getContentPane().add(btnLogin, gridBagConstraints);

        btnCancel.setBackground(new java.awt.Color(51, 102, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 37, 20, 20);
        getContentPane().add(btnCancel, gridBagConstraints);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/symbol.png"))); // NOI18N
        jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     *
     * @return the boolean value
     */
    public boolean validdate() {
        if (!(txfUserName.getText().equalsIgnoreCase("")) && !(new String(pwfPassword.getPassword()).equalsIgnoreCase(""))) {
            return true;
        }
        return false;
    }
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        main.mniLogin.setEnabled(true);
        //main.cbbAccount.setEnabled(true);
        dispose();


    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        if (!validdate()) {
            JOptionPane.showMessageDialog(this, "Please input userName and Password!");
        } else {
            String password = new String(pwfPassword.getPassword());
            if (manaBuz.login(txfUserName.getText(), password)) {
                main.lblUser.setText(txfUserName.getText());
                //enable appropriate functions
                String permissionName = manaBuz.findPermissionName(txfUserName.getText());
                if (permissionName.equalsIgnoreCase("PRODUCT MASTER")) {
                    main.btnProduct.setEnabled(true);
                } else if (permissionName.equalsIgnoreCase("CAMPAIGN MASTER")) {
                    main.btnCampaign.setEnabled(true);
                    main.btnEstimateBudget.setEnabled(true);
                    main.btnActualBudget.setEnabled(true);
                } else if (permissionName.equalsIgnoreCase("PRODUCT & CAMPAIGN MASTER")) {
                    main.btnProduct.setEnabled(true);
                    main.btnCampaign.setEnabled(true);
                    main.btnEstimateBudget.setEnabled(true);
                    main.btnActualBudget.setEnabled(true);
                } else if (permissionName.equalsIgnoreCase("ADMIN")) {
                    main.mnuManagement.setEnabled(true);
                    main.mniAccountMana.setEnabled(true);
                }
                main.btnReport.setEnabled(true);
                main.mniLogin.setEnabled(false);
                // main.btnLogin.setEnabled(false);
                main.mniLogOut.setEnabled(true);
                main.cbbAccount.setEnabled(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "userName or password incorrect!");

            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void pwfPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwfPasswordKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btnLoginActionPerformed(null);
        }
    }//GEN-LAST:event_pwfPasswordKeyPressed

    private void txfUserNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txfUserNameKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btnLoginActionPerformed(null);
        }
    }//GEN-LAST:event_txfUserNameKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                LoginDlg dialog = new LoginDlg(main);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                public void windowClosing(java.awt.event.WindowEvent e) {
//                    main.btnLogin.setEnabled(true);
//                    main.mniLogin.setEnabled(true);
//                    System.exit(0);
//                   }
//                 });
//                dialog.setVisible(true);
//
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JPasswordField pwfPassword;
    private javax.swing.JTextField txfUserName;
    // End of variables declaration//GEN-END:variables
}
